<template>
<div>
  <a-modal
    :title="`积分要求${model && model.id > 0 ? '编辑' : '新增'}`"
    :width="450"
    :visible="visible"
    :confirmLoading="loading"
    @ok="
      () => {
        $emit('ok')
      }
    "
    @cancel="
      () => {
        $emit('cancel')
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form" :labelCol="{ sm: { span: 8 } }" :wrapperCol="{ sm: { span: 16 } }">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item :label="`${it.name} 积分要求`" v-for="(it,index) in levelList" :key="index">
          <!-- <span>{{`${JSON.stringify(it)}`}}</span> -->
          <a-input
            v-decorator="[it.id+'']"
            placeholder="请输入积分要求"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
  </div>
</template>

<script>
import pick from 'lodash.pick'
import { projectTypeListByNoAuth, NoAuthLevel } from '@/api/noAuth'

// 表单字段
const fields = ['projectId', 'score_1', 'score_2', 'score_3', 'score_4']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    levelList: {
      type: Array,
      default: () => [],
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    return {
      form: this.$form.createForm(this),
      projectType: [],
      data:[],
    }
  },
  created() {
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', (n) => {
      console.log(n,'nnnn')
      if(!n){
        this.form.resetFields()
      }
      setInterval(()=>{
        this.model && this.form.setFieldsValue(n)
      },100)
      
    })
    this.getProjectTypeList()
  },
  methods: {
    getProjectTypeList() {
      projectTypeListByNoAuth({ pageNum: 1, pageSize: 100 }).then((res) => {
        this.projectType = res.data.data.filter((v) => v.id == 17)
      })
    },
    getLevelList() {
      NoAuthLevel({ pageNum:1, pageSize:100 })
      .then( res => {
        this.levelList = res.data
      })
    }
  },
}
</script>
